import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Component({
  selector: 'app-work-modal',
  templateUrl: './work-modal.component.html',
  styleUrls: ['./work-modal.component.scss']
})
export class WorkModalComponent implements OnInit {
  // 输出点击事件新增修改成功调用 父组件初始化数据方法 this.initData.emit();
  @Output() initData = new EventEmitter<string>();
  // 表单对象
  @Input() workForm: any;
  // 表单类型 1.新增 2.修改
  @Input() workType: any;
  // temp
  @Input() modal: any;
  // @Input() initData: any;
  // 初始化课程列表
  public courseList: any;
  constructor(private http: HttpClient) { }

  ngOnInit(): void {
    this.initCourse();
  }
  /*
  * 获取课程列表
  * */
  initCourse(): void{
    this.http.post('/api/selectCourse', {courseName: ''}, {headers: {'Content-Type': 'application/json'}})
      .subscribe((res: any) => {
        this.courseList = res;
        console.log('this.courseList', this.courseList);
      });
  }
  /*
  * 保存作业
  * */
  saveWork(): void{
    console.log('this.workForm.value', this.workForm.value);
    if (this.workType === 1){ // 新增
      this.http.post('/api/addWork', this.workForm.value, {headers: {'Content-Type': 'application/json'}})
        .subscribe((res: any) => {
          if (res) {
            this.initData.emit();
            this.modal.dismiss('Cross click');
          }
        });
    } else{ // 修改
      this.http.post('/api/updateWork', this.workForm.value, {headers: {'Content-Type': 'application/json'}})
        .subscribe((res: any) => {
          if (res) {
            this.initData.emit();
            this.modal.dismiss('Cross click');
          }
        });
    }
  }
}
